package com.resou.reader.discovery.choicenes;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.BookLabelBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.StoreService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.utils.UserUtil;
import com.resou.reader.view.booklebel.LabelBean;
import com.resou.reader.view.booklebel.LabelNameBean;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.observers.BlockingBaseObserver;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description:
 * CreateTime: 2019-03 -23
 **/
public class ChoiceneBookPresenter extends ResouBasePresenter<IChoiceneBookContract.IChoiceneBookView> implements IChoiceneBookContract.IChoiceneBookPresenter {


    StoreService storeService;

    public ChoiceneBookPresenter(IChoiceneBookContract.IChoiceneBookView view) {
        super(view);
        storeService = ApiImp.getInstance().getService(StoreService.class);
    }

    /**
     * 查询标签
     *
     * @param gender
     */
    @Override
    public synchronized void quearLabel(int gender) {
        storeService.quearLabel(gender, UserUtil.getToken()).subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers()).subscribe(new Observer<Result<List<BookLabelBean>>>() {
            @Override
            public void onSubscribe(Disposable d) {
                addCompositeDisposable(d);
            }

            @Override
            public void onNext(Result<List<BookLabelBean>> listResult) {
                if (listResult.getData().size() > 0) {
                    mView.showLabel(listResult.getData());
                } else {
                    mView.showError(new Throwable("暂无标签信息"));
                }
                mView.showContent();
            }

            @Override
            public void onError(Throwable e) {
//                mView.showError(e);
                mView.showContent();
                mView.showError(new Throwable("网络异常"));
            }

            @Override
            public void onComplete() {
                mView.showContent();
            }
        });
    }
}
